package vcp.client.GUI.mvc.model;

import vcp.client.forms.PeriodicForm;
import vcp.client.interfaces.IObservable;
import vcp.client.reports.ReportAbstract;
import vcp.client.request.PeriodicReportRetrieveRequest;
import vcp.client.request.PeriodicReportSaveRequest;
import vcp.entity.Site;
import vcp.entity.Worker;

/**
 * 
 * @author VCP team
 *         <p>
 *         Model responsible holding,retrieving and saving reports
 *         </p>
 * 
 */
public class UIQuarterReportsModel extends UIAbstractModel
{

	// ****************************************************************************************************************************
	// Setter-Getter methods
	// ****************************************************************************************************************************
	/** instance of Worker **/
	private Worker siteManager;

	public Worker getSiteManager ()
	{
		return this.siteManager;
	}

	public void setSiteManager ( Worker siteManager )
	{
		this.siteManager = siteManager;
	}

	/** reference to received DB's received report **/
	private ReportAbstract report;

	public ReportAbstract getReport ()
	{
		return this.report;
	}

	/** reference to current requested period **/
	private PeriodicForm form;

	/**
	 * UIQuarterReportsModel constructor
	 * 
	 * @param model
	 *            reference to main client
	 */
	public UIQuarterReportsModel ( IObservable model )
	{
		super(model);
	}

	// ****************************************************************************************************************************
	// Public methods
	// ****************************************************************************************************************************
	/**
	 * requesting to save current report
	 */
	public void savePeriodicReport ()
	{
		this.form.setSiteEnum(this.getWorkerSite().getSite());
		PeriodicReportSaveRequest request = new PeriodicReportSaveRequest(this.form);
		this.sendRequest(request, true);
	}

	/**
	 * requesting periodic data report
	 * 
	 * @param form
	 *            containing dates period and report type
	 */
	public void retrievePeriodicReport ( PeriodicForm form )
	{
		this.form = form;
		form.setSiteEnum(this.getWorkerSite().getSite());
		PeriodicReportRetrieveRequest request = new PeriodicReportRetrieveRequest(form);
		this.sendRequest(request, true);
	}

	/**
	 * 
	 * @return site to the manager
	 */
	private Site getWorkerSite ()
	{
		return this.getSiteManager().getSites().get(0);
	}

	// ****************************************************************************************************************************
	// UIAbstractModel methods
	// ****************************************************************************************************************************
	@Override
	public void clearModel ()
	{
		this.siteManager = null;
		this.report = null;
	}

	// ****************************************************************************************************************************
	// Observer methods
	// ****************************************************************************************************************************
	@Override
	public void update ( Object data )
	{
		// received new report
		if ( data instanceof ReportAbstract )
		{
			this.report = (ReportAbstract) data;
			if ( this.report.isEmptyReport() )
			{
				this.form = null;
			}
		}
		// report save was successful
		else if ( data instanceof Boolean )
		{
			this.report = null;
			this.form = null;
		}

		// notify controllerDelegate on data change
		this.controllerDelegate.updateControllerDelegateWithData(data);
		this.controllerDelegate.loadingStop();
	}

}
